
package ca.ualberta.cs.CLRServer.resources;

import java.util.ArrayList;
import java.util.List;

import javax.ws.rs.Consumes;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.PUT;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.QueryParam;
import javax.ws.rs.core.MultivaluedMap;
import javax.ws.rs.core.Response;

import uk.ac.liv.c3connector.AnnotationModel;
import uk.ac.liv.c3connector.DistributedPersonalAnnos;

import ca.ualberta.cs.BibTex.BibTex;
import ca.ualberta.cs.CLRDB.User;
import ca.ualberta.cs.CLRServer.Interface.PostContent;
import ca.ualberta.cs.CLRServer.Interface.SecureResource;
import ca.ualberta.cs.CLRServer.Interface.ServerInterface;

     @Path("/publishService")
     public class AcceptAnnotations extends SecureResource{         
    	 
        /*@GET 
        @Produces("text/plain")
        public String getClichedMessage() {
            // Return some cliched textual content
            return "Hello World";
        } */
    	 
    	 @GET
         @Produces("application/json")
         public List<JAXBBean> getClichedMessage() {
         	JAXBBean request = new JAXBBean();
     		
     		request.setKey("key1");
     		request.setVal("val1");

     		List<JAXBBean> reqArr = new ArrayList<JAXBBean>();
     		
     		reqArr.add(request);
     		
     		JAXBBean request2 = new JAXBBean();
     		request2.setKey("key2");
     		request2.setVal("val2");
     		reqArr.add(request2);
     		
     		JAXBBean request3 = new JAXBBean();
     		request3.setKey("key3");
     		request3.setVal("val3");
     		reqArr.add(request3);
     		
     		return reqArr;
         }
        
        @POST 
        @Consumes("application/json")
        //@Produces("text/plain")
        public String postAnnotation(List<JAXBBean> requestArr){
        	
        	checkBufferOverflow(requestArr,3);
        	checkRequiredKeys(requestArr, "anno", "password","username");
        	        	
        	String anno = "";
        	String username = "";
        	String pass = "";
        	
        	for(int i = 0 ; i < requestArr.size() ; i++ ){
        		if( requestArr.get(i).getKey().equals("anno") )
        			anno = requestArr.get(i).getVal();
        		else if( requestArr.get(i).getKey().equals("password") )
        			pass = requestArr.get(i).getVal();
        		else if( requestArr.get(i).getKey().equals("username") )
        			username = requestArr.get(i).getVal();
        	}
        	
        	try{
        		checkPostLimit(username, PostContent.annotation);
        	}
        	catch(Exception e){
        		return String.valueOf(-1);
        	}
        	
        	AnnotationModel am;
        	try{
        		am = ServerInterface.ams.parse(anno);
        		/*System.out.println("***************");
        		System.out.println("isreply:"+am.isReplyToSth());
        		System.out.println("replytofab:"+am.replyToFabId);
        		System.out.println();        		
        		System.out.println("annobody:"+am.annotationBody);
        		System.out.println();
        		System.out.println("****************");
        		*/
        		/*if(am.resourceUri.startsWith("file:/"))
        			am.resourceUri = DistributedPersonalAnnos.userid + ":" + am.resourceUri;*/
        		
        	}catch(Exception e){
        		return "-1";
        	}
        	
        	/*System.out.println("username: "+username +" is adding annotations");
        	System.out.println();*/
        	
        	int state = ServerInterface.db.annoMgmt.storeOrUpdateAnnotation(am,username,pass);
        	return String.valueOf(state);
        }
        
        @Path("/del")
        @POST
        @Consumes("application/json")
        public String deleteAnnotation(List<JAXBBean> requestArr){
        	
        	checkBufferOverflow(requestArr,3);
        	checkRequiredKeys(requestArr, "password", "username");
        	
        	String annoIdFab = "";
        	String username = "";
        	String pass = "";
        	Integer annoId = null;
        	
        	for(int i = 0 ; i < requestArr.size() ; i++ ){
        		if( requestArr.get(i).getKey().equals("annoIdFab") )
        			annoIdFab = requestArr.get(i).getVal();
        		else if( requestArr.get(i).getKey().equals("annoId") ){
        			try{
        			annoId = Integer.parseInt(requestArr.get(i).getVal());
        			}catch(Exception e){
        				System.out.println("problem with parsing id of the annotation to be deleted, request cancelled");
        				return "-1";
        			}
        		}
        		else if( requestArr.get(i).getKey().equals("password") )
        			pass = requestArr.get(i).getVal();
        		else if( requestArr.get(i).getKey().equals("username") )
        			username = requestArr.get(i).getVal();        		        	
        	}
        	
        	/*System.out.println("username: "+username+" is deleting an annotation");
        	System.out.println();*/
        	
        	int state;
        	if( annoId != null )
        		state = ServerInterface.db.annoMgmt.deleteAnnotation(annoId,username,pass);
        	
        	else
        		state = ServerInterface.db.annoMgmt.deleteAnnotationByFabId(annoIdFab, username, pass);
        	
        	//System.out.println("*********");
        	//System.out.println(state);
        	return String.valueOf(state);
        }
        
        @Path("/update")
        @POST 
        @Consumes("application/json")
        //@Produces("text/plain")
        public String updateAnnotation(List<JAXBBean> requestArr){
        	
        	checkBufferOverflow(requestArr,3);
        	checkRequiredKeys(requestArr, "updated", "password", "username");
        	        	
        	String anno_rep = "";
        	String username = "";
        	String pass = "";
        	
        	for(int i = 0 ; i < requestArr.size() ; i++ ){
        		if( requestArr.get(i).getKey().equals("updated") )
        			anno_rep = requestArr.get(i).getVal();
        		else if( requestArr.get(i).getKey().equals("password") )
        			pass = requestArr.get(i).getVal();
        		else if( requestArr.get(i).getKey().equals("username") )
        			username = requestArr.get(i).getVal();
        	}
        	
        	AnnotationModel am;
        	try{
        		am = ServerInterface.ams.parse(anno_rep);
        	}catch(Exception e){
        		return "-1";
        	}
        	
        	System.out.println("username: "+username +" is updating annotations");
        	System.out.println();
        	
        	int state = ServerInterface.db.annoMgmt.storeOrUpdateAnnotation(am,username,pass);
        	return String.valueOf(state);
        }
        
    }